﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Medical.Business;
using Medical.Entity;

public partial class RequestProcessing : System.Web.UI.Page
{
    /// <summary>
    /// 出错处理
    /// </summary>
    /// <param name="args"></param>
    protected override void OnError(EventArgs args)
    {
        string strPost = string.Empty;
        try
        {
            string[] postList = Request.Form.AllKeys;
            foreach (string strVal in postList)
            {

                strPost += strVal + ":" + Request.Form[strVal].Trim() + " ";
            }
        }
        catch { }
        //记录日记
        Common.Log(strPost + "出错提示：" + Context.Error.Message + "--" + Common.GetVistorsIP());
        string result = "{\"result\":\"-400\"}";
        Response.Write(result);
        Response.End();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //处理请求
            Ajax();
        }
    }

    #region ajax请求处理
    /// <summary>
    /// ajax请求处理
    /// </summary>
    protected void Ajax()
    {
        string strResult = string.Empty;
        string strAction = Common.GetQueryValue("action", 1);
        if (strAction != string.Empty)
        {
            switch (strAction)
            {
                //新增问题
                case "faq":
                    strResult = InsertFaq();
                    break;
                case "countday":
                    strResult = CountDay();
                    break;
            }
        }
        if (strResult == string.Empty)
            strResult = "{result:-400}";
        Response.Write(strResult);
        Response.End();
    }
    #endregion

    /// <summary>
    /// 新增问题
    /// </summary>
    /// <returns></returns>
    protected string InsertFaq()
    {
        string strTitle = Common.GetQueryValue("title", 1);
        if (strTitle == String.Empty)
        {
            return "{\"result\":\"0\",\"msg\":\"notitle\"}";
        }
        string strInfo = Common.GetQueryValue("info", 1);
        if (strInfo == String.Empty)
        {
            return "{\"result\":\"0\",\"msg\":\"noinfo\"}";
        }
        string strCode = Common.GetQueryValue("code", 1).ToUpper();
        if (strInfo == String.Empty)
        {
            return "{\"result\":\"0\",\"msg\":\"nocode\"}";
        }
        if (Session["WebCheckCode"] == null || strCode != Session["WebCheckCode"].ToString())
        {
            return "{\"result\":\"0\",\"msg\":\"notcode\"}";
        }
        string strPhone = Common.GetQueryValue("phone", 1);
        string strEmail = Common.GetQueryValue("email", 1);
        FeedBackInfo Info = new FeedBackInfo();
        Info.ParentID = 0;
        Info.Phone = strPhone;
        Info.ReType = 0;
        Info.Title = strTitle;
        Info.SortID = Convert.ToInt32(Common.GetQueryValue("sortid", 1));
        Info.Email = strEmail;
        Info.Info = strInfo;
        bool bResult = new BisFeedBack().Insert(Info);
        return bResult ? "{\"result\":\"1\"}" : "{\"result\":\"0\"}";
    }

    protected string CountDay()
    {
        string strYear = Common.GetQueryValue("year", 1);
        string strMonth = Common.GetQueryValue("month", 1);
        string strDay = Common.GetQueryValue("day", 1);

        DateTime dDay;
        bool bResult = DateTime.TryParse(strYear + "-" + strMonth + "-" + strDay, out dDay);
        if (bResult)
        {
            TimeSpan ts = DateTime.Now - dDay;
            if (ts.Days > 0)
            {
                return "{\"result\":\"1\",\"msg\":\"" + ts.Days.ToString() + "\"}";
            }
            else
            {
                return "{\"result\":\"0\",\"msg\":\"日期应早于今天！\"}";
            }
        }
        else
        {
            return "{\"result\":\"0\",\"msg\":\"填写日期不正确！\"}";
        }
    }
}